package com.neudu.glsb.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neudu.glsb.commons.JSONReturn;
import com.neudu.glsb.pojo.Activities;
import com.neudu.glsb.service.ActivitiesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class ActivitiesController {

    @Autowired
    private ActivitiesService activitiesService;
    @Autowired
    private JSONReturn jsonReturn;

    @PostMapping("/get_all_activities")
    public String getAll(@RequestBody Map<String, Object> pageInfoMap) {
        try {
            Integer pageNum = (Integer) pageInfoMap.get("pageNum");
            Integer pageSize = (Integer) pageInfoMap.get("pageSize");
            PageHelper.startPage(pageNum, pageSize);
            List<Activities> list = activitiesService.list();
            PageInfo<Activities> pageInfoReturn = new PageInfo<>(list);
            return jsonReturn.returnSuccess(pageInfoReturn);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @PostMapping("/get_activity_by")
    public String getByCondition(@RequestBody Map<String, Object> conditionMap) {
        try {
            Integer pageNum = (Integer) conditionMap.get("pageNum");
            Integer pageSize = (Integer) conditionMap.get("pageSize");
            String activityName = (String) conditionMap.get("activityName");

            PageHelper.startPage(pageNum, pageSize);
            LambdaQueryWrapper<Activities> queryWrapper = new LambdaQueryWrapper<>();
            if (!ObjectUtils.isEmpty(activityName)) {
                queryWrapper.like(Activities::getActivityName, activityName);
            }

            List<Activities> activities = activitiesService.list(queryWrapper);
            PageInfo<Activities> pageInfo = new PageInfo<>(activities);
            return jsonReturn.returnSuccess(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @PostMapping("/add_one_activity")
    public String addOne(@RequestBody Activities activity) {
        try {
            activitiesService.save(activity);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @PostMapping("/getactivityby_id")
    public String getById(@RequestBody Map<String, Integer> idMap) {
        try {
            Integer id = idMap.get("id");
            if (ObjectUtils.isEmpty(id)) {
                return jsonReturn.returnError("ID不能为空");
            }
            Activities activity = activitiesService.getById(id);
            return jsonReturn.returnSuccess(activity);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @PostMapping("/modifyactivityby_id")
    public String modifyById(@RequestBody Activities activity) {
        try {
            if (ObjectUtils.isEmpty(activity.getId())) {
                return jsonReturn.returnError("更新失败，id 不能为空");
            }
            boolean success = activitiesService.updateById(activity);
            if (success) {
                return jsonReturn.returnSuccess();
            } else {
                return jsonReturn.returnError("更新失败，未找到对应的活动信息");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @PostMapping("/removeactivity_by_id")
    public String removeById(@RequestBody Map<String, Integer> idMap) {
        try {
            Integer id = idMap.get("id");
            if (ObjectUtils.isEmpty(id)) {
                return jsonReturn.returnError("ID不能为空");
            }
            boolean success = activitiesService.removeById(id);
            if (success) {
                return jsonReturn.returnSuccess();
            } else {
                return jsonReturn.returnError("删除失败，未找到对应的活动信息");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }
}